var id = 0;
var sizeNameNode = [];    // 发货尺码

var thisPage = {
    // 初始化页面
    OnPageLoad: function () {
        $.ajax({
            type: "POST",
            url: window.basePath + "/trade/shipping_order/print/fetch.html",
            timeout: 30 * 1000,
            data: id,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            let item = data.HeaderEntity;
            $("#CustomerName").text(item.customerName);
            $("#SoNo").text(item.soNo);
            $("#DeliveryDate").text(item.dateOfDelivery);
            $("#GarmentFactoryName").text(item.garmentFactoryName);
            $("#InCase").text(item.inCase);
            $("#PreparedBy").text(item.updatedByName);
            $("#PrintedDate").text(getCurrentDate());                 // 打印日期

            sizeNameNode = data.SizeNameNode;
            thisPage.SetTableHeader();
            thisPage.SetLinesData(data.LinesList);
            thisPage.SetLinesDescData(data.LinesDescList);
        }).fail(function (data) {
            ajaxError(data, 1);
        }).always(function () {
            $('#loading').hide();
        });
    },
    // 设置表头
    SetTableHeader: function () {
        let content = '';
        if (sizeNameNode.length > 0) {
            for (let i = 0; i < sizeNameNode.length; i++) {
                content += '<td class="cell _header" style="text-align:center;">' + sizeNameNode[i] + '</td>';
            }
        }
        content += '<td class="cell _header" style="width:18px;"></td>';
        $("#tabData thead tr").append(content);
    },
    SetLinesData: function (itemList) {
        $('#tabData tbody').empty();
        // 得到数据列表
        if (itemList === null || itemList.length === 0) {
            return false;
        }
        let content_01 = '';
        for (let i = 0; i < itemList.length; i++) {
            content_01 += '<tr>';
            content_01 += '<td class="cell" style="text-align:center; width:36px;">' + itemList[i].cartonNo + '</td>';
            content_01 += '<td class="cell" style="width:60px; text-align:center;">' + itemList[i].destination + '</td>';
            content_01 += '<td class="cell" style="width:120px; text-align:center;">' + itemList[i].styleNo + '</td>';
            content_01 += '<td class="cell" style="width:40px; text-align:center;">' + itemList[i].colourName + '</br>' + itemList[i].colourNumber + '</td>';
            for (let j = 0; j < sizeNameNode.length; j++) {
                let _sizeName = sizeNameNode[j];
                let _t = false;
                $.each(itemList[i].inBoxMemoJson, function (index, data) {
                    if (data.SizeName === _sizeName) {
                        content_01 += '<td class="cell">' + formatNullData(data.InBox) + '</td>';
                        _t = true;
                        return false;
                    }
                });
                if (_t === false)
                    content_01 += '<td class="cell"></td>';
            }
            content_01 += '</tr>';
        }
        $('#tabData tbody').append(content_01);
        $('#tabData tbody tr:last td').css({"border-bottom": "none"});
        setOneTableRowColor("tabData");
    },
    SetLinesDescData: function (itemList) {
        $('#tabDescData tbody').empty();
        // 得到数据列表
        if (itemList === null || itemList.length === 0) {
            return false;
        }
        let content_01 = '';
        for (let i = 0; i < itemList.length; i++) {
            content_01 += '<tr>';
            content_01 += '<td class="cell" style="text-align:center; width:36px;">' + itemList[i].cartonNo + '</td>';
            content_01 += '<td class="cell" style="width:60px; text-align:center;">' + itemList[i].destination + '</td>';
            content_01 += '<td class="cell" style="width:120px; text-align:center;">' + itemList[i].styleNo + '</td>';
            content_01 += '<td class="cell" style="width:40px; text-align:center;">' + itemList[i].colourName + '</br>' + itemList[i].colourNumber + '</td>';
            content_01 += '<td class="cell" style="width:70px; text-align:center;">' + itemList[i].sizeName + '</td>';
            content_01 += '<td class="cell" style="width:100px; text-align:center;" colspan="3">' + itemList[i].inBox + '</td>';
            content_01 += '<td class="cell" style="width:360px; text-align:center;" colspan="5">' + itemList[i].description + '</td>';
            content_01 += '</tr>';
        }
        $('#tabDescData tbody').append(content_01);
        $('#tabDescData tbody tr:last td').css({"border-bottom": "none"});
        setOneTableRowColor("tabDescData");
    },
    ExportToExcel: function (table, sheetname, filename) {
        let uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)));
                },
                format = function (s, c) {
                    return s.replace(/{(\w+)}/g,
                            function (m, p) {
                                return c[p];
                            });
                };
        if (!table.nodeType)
            table = document.getElementById(table);
        let ctx = {worksheet: sheetname || 'Worksheet', table: table.innerHTML};
        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = filename; // 这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了
        document.getElementById("dlink").click();
        /*
         return function (table, name) {
         if (!table.nodeType)
         table = document.getElementById(table);
         let ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
         window.location.href = uri + base64(format(template, ctx));
         };*/
    },
    Export() {
        $("#export_form").attr("action", parent.basePath + "/trade/shipping_order/export_to_excel.html");
        $("#ExportHeaderId").val(id);
        $("#ExportFlag").val(1);
        $("#export_form").submit();
    },
    Export02() {
        $("#export_form").attr("action", parent.basePath + "/trade/shipping_order/export_to_excel.html");
        $("#ExportHeaderId").val(id);
        $("#ExportFlag").val(2);
        $("#export_form").submit();
    },
    Export03() {
        $("#export_form").attr("action", parent.basePath + "/trade/shipping_order/export_to_excel.html");
        $("#ExportHeaderId").val(id);
        $("#ExportFlag").val(3);
        $("#export_form").submit();
    }
};
//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    id = jQuery.url.param("id");

    // 用于导出excel文件
    $("#export_iframe").remove();
    let $iFrame = $('<iframe id="export_iframe" name="_export_iframe" style="position:absolute; top:-9999px; left:-9999px" src="javascript:false"></iframe>');
    $("body").append($iFrame);
    let $form = $('<form id="export_form" name ="_export_form" method="POST" action ="" target="_export_iframe" style="position:absolute; top:-1200px; left:-1200px"></form>');
    $('<input type="hidden" id="ExportHeaderId" name="_ExportHeaderId" value="" />').appendTo($form);
    $('<input type="hidden" id="ExportFlag" name="_ExportFlag" value="" />').appendTo($form);
    $("body").append($form);
    // 监听iframe回调（submit()后设置iFrame加载完成事件，用于获取表单提交成功后的结果,iFrame.load(loadFunc());）
    $iFrame.on("load", function () {
        let contentWin = document.getElementById("export_iframe").contentWindow;
        let backBodyText = contentWin.document.body.innerText;
    });

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    let h = $(window).height();
    let _data_h = h - 241 - 120;
    let _mh = (_data_h - _data_h % 19) + 'px';
    $('#tabData tbody').css({"max-height": _mh});
}